package com.job;


import com.enums.CommonEnum;
import com.repository.mapper.PcMessageMapper;
import com.repository.po.PcMessage;
import com.service.SocketService;
import com.util.WebsocketUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class Task {

    @Resource
    PcMessageMapper pcMessageMapper;

    @Scheduled(cron = "*/20 * * * * ?")
    public void pcStatusSync() {
        List<PcMessage> pcMessages = pcMessageMapper.selectList(null);
        for (PcMessage pcMessage : pcMessages) {
            //如果最后一次心跳时间超过20秒，说明pc端已经离线
            Date updateTime = pcMessage.getUpdateTime();
            long time = new Date().getTime() - updateTime.getTime();
            if (time > 100000) {
                log.info("机器ID: " + pcMessage.getPcUniqueKey() + " 已离线");
                pcMessage.setPcStatus(CommonEnum.PC_STATUS_ERROR.getCode());
                pcMessageMapper.updateById(pcMessage);
            }
        }
    }
}
